from django.db import models

# Create your models here.
class Node(models.Model):
    name = models.CharField(max_length=100, unique=True)
    ip_address = models.GenericIPAddressField()

    NODE_ROLES = (
        ("M", "Managing"),
        ("C", "Computing"),
    )
    role = models.CharField(max_length=1, choices=NODE_ROLES, blank=False)

    cpu = models.IntegerField(default=0)
    memory = models.FloatField(default=0)
    io_read = models.FloatField(default=0)
    io_write = models.FloatField(default=0)
    net_send = models.FloatField(default=0)
    net_receive = models.FloatField(default=0)



class Application(models.Model):
    name = models.CharField(max_length=100, unique=True)
    definition_file = models.FilePathField(blank=True)

    cpu_limit = models.IntegerField(default=0)
    memory_limit = models.FloatField(default=0)
    io_read_limit = models.FloatField(default=0)
    io_write_limit = models.FloatField(default=0)
    net_send_limit = models.FloatField(default=0)
    net_receive_limit = models.FloatField(default=0)



class Task(models.Model):
    application = models.ForeignKey("Application")
    node = models.ForeignKey("Node")

    name = models.CharField(max_length=100, unique=True)
    start_at = models.DateTimeField(auto_now_add=True)



class Container(models.Model):
    task = models.ForeignKey("Task")
    image = models.ForeignKey("Image")

    name = models.CharField(max_length=100)
    command = models.CharField(max_length=100, blank=True)
    volumes = models.CharField(max_length=100, blank=True)
    ports = models.CharField(max_length=100, blank=True)

    CONTAINER_STATUS = (
        ("R", "Running"),
        ("S", "Stopped"),
    )
    status = models.CharField(max_length=1, choices=CONTAINER_STATUS, blank=False)



class Image(models.Model):
    name = models.CharField(max_length=100, unique=True)
    tag = models.CharField(max_length=100, blank=True)
    size = models.FloatField(default=0)

    IMAGE_USAGES = (
        ("B", "Base"),
        ("A", "Application"),
    )
    usage = models.CharField(max_length=1, choices=IMAGE_USAGES, blank=False)



class ContainerResourceConsumption(models.Model):
    container = models.ForeignKey("Container")

    cpu_core = models.IntegerField(default=0)
    cpu_time = models.IntegerField(default=0)
    memory_cap = models.FloatField(default=0)
    page_fault = models.IntegerField(default=0)
    io_read = models.FloatField(default=0)
    io_write = models.FloatField(default=0)
    net_send = models.FloatField(default=0)
    net_receive = models.FloatField(default=0)
    net_packets = models.IntegerField(default=0)
    timestamp = models.DateTimeField(auto_now_add=True)